package com.firemoss.atonement.metadata
{
	import flash.utils.Dictionary;
	
	import org.as3commons.reflect.Type;

	public class PropertyMetadata
	{
		private var type : Type
			
		public var properties : Dictionary = new Dictionary()

		public function PropertyMetadata( type : Type )
		{
			this.type = type;
		}
		
		public function containsProperty( name : String, ignoreCase : Boolean = false ) : Boolean
		{
			if ( !ignoreCase )
			{
				return properties[ name ] != null
			}
			else
			{
				return findPropertyIgnoringCase( name ) != null
			}
					
		}
		
		public function getProperty( name : String, ignoreCase : Boolean = false ) : Property
		{
			var property : Property = properties[ name ]
				
			if ( !property && ignoreCase )
			{
				property = findPropertyIgnoringCase( name )
			}
			
			if ( !property )
			{
				throw new Error( type.fullName + " doesn't contain a property named " + name );
			}
			
			return property
		}
		
		public function addProperty( property : Property ) : void
		{
			properties[ property.name ] = property
		}
		
		private function findPropertyIgnoringCase( name : String ) : Property
		{
			var uName : String = name.toUpperCase()
				
			for ( var key : String in properties )
			{
				if ( key.toUpperCase() == uName )
				{
					return properties[ key ]
				}
			}
			
			return null
		}
	}
}